/****************************************************************************
 * Chariott - Continuous Integration Testing Tool
 * Zaid Mohsin
 * The University of York
 * 2012/2013
 * MSc in Information Technology
 * All rights reserved.
 ****************************************************************************/
package chariott.main;

import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.List;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.taskdefs.optional.junit.AggregateTransformer;
import org.apache.tools.ant.taskdefs.optional.junit.XMLResultAggregator;
import org.apache.tools.ant.types.FileSet;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

/**
 * class that defines the Test Aggregation and Report generation utility of
 * ChARIOTT.
 * 
 * @author Zaid
 * 
 */
public class TestAggregator {

	File testFolder;

	public TestAggregator(File testFolder) {

		this.testFolder = testFolder;
	}

	/**
	 * method to create a report from the test results folder
	 * 
	 */
	public void generateReport() {
		Project project = new Project();
		project.setName("myproject");
		project.init();

		Target target = new Target();
		target.setName("junitreport");
		project.addTarget(target);

		FileSet fs = new FileSet();
		fs.setDir(testFolder);
		fs.createInclude().setName("*.xml");
		XMLResultAggregator aggregator = new XMLResultAggregator();
		aggregator.setProject(project);
		aggregator.addFileSet(fs);
		AggregateTransformer transformer = aggregator.createReport();
		transformer.setTodir(new File("./testreport"));

		target.addTask(aggregator);
		project.executeTarget("junitreport");
	}

	/**
	 * method to add all the new tests and their run times to the ChARIOTT
	 * database.
	 * 
	 * @throws JDOMException
	 * @throws IOException
	 */
	public void addTests() throws JDOMException, IOException {
		Object[][] myTests = null;

		SAXBuilder builder = new SAXBuilder();
		Document document = (Document) builder.build("TESTS-TestSuites.xml");
		Element rootNode = document.getRootElement();
		List list = rootNode.getChildren("testsuite");

		// get the current time
		long timestamp = Calendar.getInstance().getTimeInMillis();

		// initialize the tests to add to the DB
		myTests = new Object[list.size()][3];

		for (int i = 0; i < list.size(); i++) {

			Element node = (Element) list.get(i);

			myTests[i][0] = new Timestamp(timestamp);
			myTests[i][1] = node.getAttributeValue("name");
			myTests[i][2] = Double.parseDouble(node.getAttributeValue("time"));

		}

		// add the new fetched tests to the db
		TestsDBConnector mydbconnector = new TestsDBConnector();
		mydbconnector.addTests(myTests);
		mydbconnector.closeConnection();
	}

	public static void main(String[] args) {
		TestAggregator ta = new TestAggregator(new File(
				"C:\\Users\\otonik\\workspace\\TestProject\\chariottResults"));
		try {
			ta.addTests();
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
